package com.yhp.web;

import com.yhp.bean.Role;
import com.yhp.service.RoleService;
import com.yhp.service.impl.RoleServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(urlPatterns = "/roleservlet")
public class RoleServlet extends HttpServlet {

    RoleService roleService=new RoleServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String key = req.getParameter("key");
        switch (key){
            case "selectall":
                selectall(req, resp);
                break;
            case "delete":
                delete(req, resp);
                break;
            case "selectbyid":
                selectbyid(req, resp);
                break;
            case "update":
                update(req,resp);
                break;
        }

    }


    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String rid = req.getParameter("rid");
        String rname = req.getParameter("rname");
        Role role = new Role();
        role.setRoleId(Integer.parseInt(rid));
        role.setRoleName(rname);

        int i = roleService.updateRole(role);
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        if(i>0){
            writer.print("<script type='text/javascript'>alert('修改成功');location.href='/roleservlet?key=selectall'</script>");
        }else{
            writer.print("<script type='text/javascript'>alert('修改失败');location.href='/roleservlet?key=selectbyid&rid="+rid+"'</script>");
        }

    }


    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String rid = req.getParameter("rid");
        int i = roleService.deleteById(Integer.parseInt(rid));
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        if(i>0){
            writer.print("<script type='text/javascript'>alert('删除成功');location.href='/roleservlet?key=selectall'</script>");
        }else{
            writer.print("<script type='text/javascript'>alert('删除失败');location.href='/roleservlet?key=selectall'</script>");
        }

    }
    protected void selectbyid(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String rid = req.getParameter("rid");
        Role role = roleService.findById(Integer.parseInt(rid));
        req.setAttribute("role",role);
        req.getRequestDispatcher("/showrole.jsp").forward(req,resp);
    }

    protected void selectall(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        List<Role> roles = roleService.selectAll();
        req.setAttribute("rolelist",roles);
        req.getRequestDispatcher("/role.jsp").forward(req,resp);

    }
}
